package cn.hiapi.shopkeeper.service.user;

import cn.hiapi.basic.ResUtils;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.service.AbsBasicService;
import cn.hiapi.shopkeeper.entity.user.SubscribeShop;
import cn.hiapi.shopkeeper.jpa.user.SubscribeShopJpa;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author AdinZ
 * @date 2022/12/31 22:27
 */
@Service
public class SubscribeShopService extends AbsBasicService<SubscribeShopJpa, SubscribeShop, Long> {

    @Transactional
    public int deleteByUid(long uid, long shopId) {
        return ResUtils.getDefault(this.repository.deleteByUid(uid, shopId));
    }

    @Transactional
    public void subscribeShop(long uid, long shopId) {
        List<SubscribeShop> list = this.findList(new QueryWrapper().eq("uid", uid).eq("shopId", shopId));
        if (list.isEmpty()) {
            SubscribeShop subscribe = new SubscribeShop();
            subscribe.setShopId(shopId);
            subscribe.setUid(uid);
            this.save(subscribe);
        }
    }
}
